package controllers

import (
	"fmt"
	"github.com/gin-gonic/gin"

	"gorm.io/gorm"
	"main.go/database"
	"main.go/model"
	"net/http"
)

func Login_port_company(c *gin.Context) {
	db := database.GetDB()
	var port_company model.Port_company
	request := make(map[string]interface{})
	c.ShouldBind(&request)
	fmt.Println(request)
	port_company.Port_company_name = request["port_company_name"].(string)
	port_company.Port_company_password = request["port_company_password"].(string)
	fmt.Println("port_company_name为")
	fmt.Println(port_company.Port_company_name)
	fmt.Println("密码为")
	fmt.Println(port_company.Port_company_password)
	if Find_port_company_password(db, port_company) == true {
		fmt.Println("登录成功")
		c.JSON(200, gin.H{
			"code": "200",
			"msg":  "ok",
		})
		// 开启登录成功后的主页面
	} else {
		c.JSON(404, gin.H{
			"code": "404",
			"msg":  "no",
		})
		fmt.Println("登录失败")
	}
}
func Find_port_company_password(db *gorm.DB, test model.Port_company) bool {
	var port_company model.Port_company
	fmt.Println(test.Port_company_name)
	fmt.Println(test.Port_company_password)
	db.Where("port_company_name = ? AND port_company_password = ?", test.Port_company_name, test.Port_company_password).First(&port_company)
	if port_company.Port_company_name != "" {
		fmt.Println(port_company.Port_company_name)
		return true
	}
	return false
}

//查找
func Find_port_company(db *gorm.DB, request model.Port_company) bool {
	var port_company model.Port_company
	db.Where("port_company_name = ?", request.Port_company_name).First(&port_company)
	if port_company.Port_company_name != "" {
		fmt.Println("查找成功")
		return true
	}
	fmt.Println("查找失败")
	return false
}

//插入
func Insert_port_company(db *gorm.DB, request model.Port_company) bool {
	if Find_port_company(db, request) == true {
		fmt.Println("该港口公司已存在，不能插入")
		return false
	}
	db.Create(&request)
	fmt.Println("插入成功")
	return true
}

//删除
func Delete_port_company(db *gorm.DB, request model.Port_company) bool {
	if Find_port_company(db, request) == false {
		fmt.Println("不存在该港口公司，无法删除")
		return false
	}
	db.Where("port_company_name = ?", request.Port_company_name).Delete(&request)
	fmt.Println("删除成功")
	return true
}

//更新
func Update_port_company(db *gorm.DB, request model.Port_company) bool {
	if Find_port_company(db, request) == false {
		fmt.Println("无该港口公司，无法更新")
		return false
	}
	db.Model(&request).Where("port_company_name = ?", request.Port_company_name).Updates(
		model.Port_company{
			Port_company_name:     request.Port_company_name,
			Port_company_password: request.Port_company_password,
		},
	)
	fmt.Println("更新成功")
	return true
}
func Web_show_port_company(c *gin.Context) {
	db := database.GetDB()
	var port_company []model.Port_company
	result := db.Table("port_companies").Find(&port_company)
	if result != nil {
		fmt.Println("!!")
	}
	fmt.Println(port_company)
	c.JSON(http.StatusOK, gin.H{
		"code":         "200",
		"port_company": port_company,
		"msg":          "成功",
	})
}
func Web_get_port_company(c *gin.Context) {
	db := database.GetDB()
	var port_company1 model.Port_company
	var port_company2 []model.Port_company
	request := make(map[string]interface{})
	c.ShouldBind(&request)
	fmt.Println(request)
	port_company1.Port_company_name = request["port_company_name"].(string)
	db.Where("port_company_name = ?", port_company1.Port_company_name).First(&port_company2)
	if port_company2[0].Port_company_name != "" {
		fmt.Println("成功查找到港口公司")
		c.JSON(200, gin.H{
			"code":         "200",
			"port_company": port_company2, //传一个对象
			"msg":          "成功",
		})
	} else {
		c.JSON(404, gin.H{
			"code":         "404",
			"port_company": port_company2, //传一个空对象
			"msg":          "no",
		})
		fmt.Println("未找到该港口公司")
	}
}
func Web_add_port_company(c *gin.Context) {
	db := database.GetDB()
	var port_company model.Port_company
	request := make(map[string]interface{})
	c.ShouldBind(&request)
	fmt.Println(request)
	port_company.Port_company_name = request["port_company_name"].(string)
	port_company.Port_company_password = request["port_company_password"].(string)
	if Insert_port_company(db, port_company) {
		c.JSON(200, gin.H{
			"code": "200",
			"msg":  "成功",
		})
		fmt.Println("添加成功")
	} else {
		c.JSON(404, gin.H{
			"code": "404",
			"msg":  "添加失败",
		})
		fmt.Println("添加失败")
	}
}
func Web_delete_port_company(c *gin.Context) {
	db := database.GetDB()
	var port_company model.Port_company
	request := make(map[string]interface{})
	c.ShouldBind(&request)
	fmt.Println(request)
	port_company.Port_company_name = request["port_company_name"].(string)
	if Delete_port_company(db, port_company) {
		c.JSON(200, gin.H{
			"code": "200",
			"msg":  "成功",
		})
		fmt.Println("删除成功")
	} else {
		c.JSON(404, gin.H{
			"code": "404",
			"msg":  "删除失败",
		})
		fmt.Println("删除失败")
	}
}
func Web_update_port_company(c *gin.Context) {
	db := database.GetDB()
	var port_company model.Port_company
	request := make(map[string]interface{})
	c.ShouldBind(&request)
	fmt.Println(request)
	port_company.Port_company_name = request["port_company_name"].(string)
	port_company.Port_company_password = request["port_company_password"].(string)
	if Update_port_company(db, port_company) {
		c.JSON(200, gin.H{
			"code": "200",
			"msg":  "成功",
		})
		fmt.Println("修改成功")
	} else {
		c.JSON(404, gin.H{
			"code": "404",
			"msg":  "修改失败",
		})
		fmt.Println("修改失败")
	}
}
